#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![feature(extern_types)]
extern "C" {
    pub type _IO_wide_data;
    pub type _IO_codecvt;
    pub type _IO_marker;
    static mut stdin: *mut FILE;
    static mut stdout: *mut FILE;
    fn setvbuf(
        __stream: *mut FILE,
        __buf: *mut libc::c_char,
        __modes: libc::c_int,
        __n: size_t,
    ) -> libc::c_int;
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
    fn perror(__s: *const libc::c_char);
    fn fgets(
        __s: *mut libc::c_char,
        __n: libc::c_int,
        __stream: *mut FILE,
    ) -> *mut libc::c_char;
    fn exit(_: libc::c_int) -> !;
    fn mmap(
        __addr: *mut libc::c_void,
        __len: size_t,
        __prot: libc::c_int,
        __flags: libc::c_int,
        __fd: libc::c_int,
        __offset: __off_t,
    ) -> *mut libc::c_void;
    fn munmap(__addr: *mut libc::c_void, __len: size_t) -> libc::c_int;
    fn epoll_create1(__flags: libc::c_int) -> libc::c_int;
    fn epoll_ctl(
        __epfd: libc::c_int,
        __op: libc::c_int,
        __fd: libc::c_int,
        __event: *mut epoll_event,
    ) -> libc::c_int;
    fn epoll_wait(
        __epfd: libc::c_int,
        __events: *mut epoll_event,
        __maxevents: libc::c_int,
        __timeout: libc::c_int,
    ) -> libc::c_int;
    fn close(__fd: libc::c_int) -> libc::c_int;
    fn sleep(__seconds: libc::c_uint) -> libc::c_uint;
    fn fork() -> __pid_t;
    fn unlink(__name: *const libc::c_char) -> libc::c_int;
    fn ftruncate(__fd: libc::c_int, __length: __off_t) -> libc::c_int;
    fn strncpy(
        _: *mut libc::c_char,
        _: *const libc::c_char,
        _: libc::c_ulong,
    ) -> *mut libc::c_char;
    fn open(__file: *const libc::c_char, __oflag: libc::c_int, _: ...) -> libc::c_int;
}
pub type size_t = libc::c_ulong;
pub type __uint32_t = libc::c_uint;
pub type __uint64_t = libc::c_ulong;
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
pub type __pid_t = libc::c_int;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
    pub _flags: libc::c_int,
    pub _IO_read_ptr: *mut libc::c_char,
    pub _IO_read_end: *mut libc::c_char,
    pub _IO_read_base: *mut libc::c_char,
    pub _IO_write_base: *mut libc::c_char,
    pub _IO_write_ptr: *mut libc::c_char,
    pub _IO_write_end: *mut libc::c_char,
    pub _IO_buf_base: *mut libc::c_char,
    pub _IO_buf_end: *mut libc::c_char,
    pub _IO_save_base: *mut libc::c_char,
    pub _IO_backup_base: *mut libc::c_char,
    pub _IO_save_end: *mut libc::c_char,
    pub _markers: *mut _IO_marker,
    pub _chain: *mut _IO_FILE,
    pub _fileno: libc::c_int,
    pub _flags2: libc::c_int,
    pub _old_offset: __off_t,
    pub _cur_column: libc::c_ushort,
    pub _vtable_offset: libc::c_schar,
    pub _shortbuf: [libc::c_char; 1],
    pub _lock: *mut libc::c_void,
    pub _offset: __off64_t,
    pub _codecvt: *mut _IO_codecvt,
    pub _wide_data: *mut _IO_wide_data,
    pub _freeres_list: *mut _IO_FILE,
    pub _freeres_buf: *mut libc::c_void,
    pub __pad5: size_t,
    pub _mode: libc::c_int,
    pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
pub type pid_t = __pid_t;
pub type uint32_t = __uint32_t;
pub type uint64_t = __uint64_t;
pub type EPOLL_EVENTS = libc::c_uint;
pub const EPOLLET: EPOLL_EVENTS = 2147483648;
pub const EPOLLONESHOT: EPOLL_EVENTS = 1073741824;
pub const EPOLLWAKEUP: EPOLL_EVENTS = 536870912;
pub const EPOLLEXCLUSIVE: EPOLL_EVENTS = 268435456;
pub const EPOLLRDHUP: EPOLL_EVENTS = 8192;
pub const EPOLLHUP: EPOLL_EVENTS = 16;
pub const EPOLLERR: EPOLL_EVENTS = 8;
pub const EPOLLMSG: EPOLL_EVENTS = 1024;
pub const EPOLLWRBAND: EPOLL_EVENTS = 512;
pub const EPOLLWRNORM: EPOLL_EVENTS = 256;
pub const EPOLLRDBAND: EPOLL_EVENTS = 128;
pub const EPOLLRDNORM: EPOLL_EVENTS = 64;
pub const EPOLLOUT: EPOLL_EVENTS = 4;
pub const EPOLLPRI: EPOLL_EVENTS = 2;
pub const EPOLLIN: EPOLL_EVENTS = 1;
#[derive(Copy, Clone)]
#[repr(C)]
pub union epoll_data {
    pub ptr: *mut libc::c_void,
    pub fd: libc::c_int,
    pub u32_0: uint32_t,
    pub u64_0: uint64_t,
}
pub type epoll_data_t = epoll_data;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct epoll_event {
    pub events: uint32_t,
    pub data: epoll_data_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct message {
    pub written: libc::c_int,
    pub text: [libc::c_char; 1020],
}
#[no_mangle]
pub unsafe extern "C" fn server(mut shm: *mut message) {
    let mut epfd: libc::c_int = epoll_create1(0 as libc::c_int);
    if epfd < 0 as libc::c_int {
        perror(b"epoll_create1\0" as *const u8 as *const libc::c_char);
        exit(1 as libc::c_int);
    }
    let mut ev: epoll_event = epoll_event {
        events: 0,
        data: epoll_data {
            ptr: 0 as *mut libc::c_void,
        },
    };
    let mut events: [epoll_event; 1] = [epoll_event {
        events: 0,
        data: epoll_data {
            ptr: 0 as *mut libc::c_void,
        },
    }; 1];
    ev.events = EPOLLIN as libc::c_int as uint32_t;
    ev.data.fd = 0 as libc::c_int;
    if epoll_ctl(epfd, 1 as libc::c_int, 0 as libc::c_int, &mut ev)
        == -(1 as libc::c_int)
    {
        perror(b"epoll_ctl: add\0" as *const u8 as *const libc::c_char);
        exit(1 as libc::c_int);
    }
    loop {
        let mut nfds: libc::c_int = epoll_wait(
            epfd,
            events.as_mut_ptr(),
            1 as libc::c_int,
            500 as libc::c_int,
        );
        if nfds == -(1 as libc::c_int) {
            perror(b"epoll_wait\0" as *const u8 as *const libc::c_char);
            exit(1 as libc::c_int);
        }
        if (*shm).written != 0 {
            printf(
                b"Server received: %s\0" as *const u8 as *const libc::c_char,
                ((*shm).text).as_mut_ptr(),
            );
            (*shm).written = 0 as libc::c_int;
        }
    };
}
#[no_mangle]
pub unsafe extern "C" fn client(mut shm: *mut message) {
    let mut buf: [libc::c_char; 1024] = [0; 1024];
    loop {
        while (*shm).written == 1 as libc::c_int {
            sleep(1 as libc::c_int as libc::c_uint);
        }
        printf(b"Enter some text: \0" as *const u8 as *const libc::c_char);
        fgets(
            buf.as_mut_ptr(),
            ::core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong
                as libc::c_int,
            stdin,
        );
        strncpy(
            ((*shm).text).as_mut_ptr(),
            buf.as_mut_ptr(),
            ::core::mem::size_of::<[libc::c_char; 1020]>() as libc::c_ulong,
        );
        (*shm).written = 1 as libc::c_int;
    };
}
unsafe fn main_0() -> libc::c_int {
    setvbuf(
        stdout,
        0 as *mut libc::c_char,
        2 as libc::c_int,
        0 as libc::c_int as size_t,
    );
    unlink(b"shm_example\0" as *const u8 as *const libc::c_char);
    let mut shm_fd: libc::c_int = open(
        b"shm_example\0" as *const u8 as *const libc::c_char,
        0o100 as libc::c_int | 0o2 as libc::c_int,
        0o666 as libc::c_int,
    );
    if shm_fd == -(1 as libc::c_int) {
        perror(b"shm open\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    if ftruncate(shm_fd, ::core::mem::size_of::<message>() as libc::c_ulong as __off_t)
        == -(1 as libc::c_int)
    {
        perror(b"ftruncate\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    let mut shm: *mut message = mmap(
        0 as *mut libc::c_void,
        ::core::mem::size_of::<message>() as libc::c_ulong,
        0x1 as libc::c_int | 0x2 as libc::c_int,
        0x1 as libc::c_int,
        shm_fd,
        0 as libc::c_int as __off_t,
    ) as *mut message;
    if shm == -(1 as libc::c_int) as *mut libc::c_void as *mut message {
        perror(b"mmap\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    (*shm).written = 0 as libc::c_int;
    let mut pid: pid_t = fork();
    if pid < 0 as libc::c_int {
        perror(b"fork\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    if pid == 0 as libc::c_int {
        client(shm);
    } else {
        server(shm);
    }
    if munmap(
        shm as *mut libc::c_void,
        ::core::mem::size_of::<message>() as libc::c_ulong,
    ) == -(1 as libc::c_int)
    {
        perror(b"munmap\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    if unlink(b"shm_example\0" as *const u8 as *const libc::c_char)
        == -(1 as libc::c_int)
    {
        perror(b"shm unlink\0" as *const u8 as *const libc::c_char);
        return 1 as libc::c_int;
    }
    return 0 as libc::c_int;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}